package com.example.expandablelistdemo;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ListAdapter extends BaseExpandableListAdapter{

	private Context mContext;
	private List<? extends ForumData> datas;
	private LayoutInflater inflater;
	
	public ListAdapter(Context context, List<? extends ForumData> datas){
		this.mContext = context;
		this.datas = datas;
		
		inflater = LayoutInflater.from(mContext);
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return datas.get(groupPosition).getDataList().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		ForumData childData = datas.get(groupPosition).getDataList().get(childPosition);
		ChildHolder childHolder = null;
		
		if(convertView == null){
			childHolder = new ChildHolder();
			convertView = inflater.inflate(R.layout.list_item_c, null);
			childHolder.c_name = (TextView) convertView.findViewById(R.id.c_name);
			convertView.setTag(childHolder);
		}else{
			childHolder = (ChildHolder) convertView.getTag();
		}
		
		childHolder.c_name.setText("--"+childData.getName());
		childHolder.c_name.setTag(childData.getId());
		
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return datas.get(groupPosition).getDataList() !=null ? datas.get(groupPosition).getDataList().size() : 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return datas.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return datas !=null ? datas.size():0;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		ForumData parentData = datas.get(groupPosition);
		ParentHolder parentHolder = null;
		if(convertView == null){
			parentHolder = new ParentHolder();
			convertView = inflater.inflate(R.layout.list_item_p, null);
			parentHolder.p_name = (TextView) convertView.findViewById(R.id.p_name);
			convertView.setTag(parentHolder);
			
		}else{
			parentHolder = (ParentHolder) convertView.getTag();
		}
		parentHolder.p_name.setText(parentData.getName());
		parentHolder.p_name.setTag(parentData.getId());
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	private class ChildHolder{
		TextView c_name;
	}
	
	private class ParentHolder{
		TextView p_name;
	}

}
